import JWT from 'jsonwebtoken'
import {getUserById} from '~/server/database/repositories/validationCodesRepository'
export default defineEventHandler(async(e)=>{
  try {
    const jwt = e.node.req.headers.authorization ?? ''
    const info = JWT.verify(jwt, process.env.JSON_SECRET)
    const {id} = info
    const user = await getUserById(id)
    if (!user) {
      return sendError(e, createError({
        statusCode: 401,
        statusMessage: '用户错误!',
      }))
    }
    return { resource: user }
  }
  catch(error){
    return sendError(e, createError('获取用户信息失败!'))
  }
})